<template>
  <div class="app-container">
    <div class="filter-container" align="left">
      <el-button class="filter-item" type="success" icon="el-icon-edit" @click="handleCreate"
        style="margin-left: 10px;">新增宠物品种</el-button>
    </div>
    <el-table :data="petBreedList" style="width: 80%;" border>
      <el-table-column label="序号" align="center">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="petbreedName" label="品种" align="center">
      </el-table-column>
      <el-table-column prop="pettypeName" label="种类" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{row}">
          <!-- <el-button size="mini" plain type="primary" @click="handleUpdate(row)">编辑</el-button> -->
          <el-button size="mini" plain type="danger" @click="handleDelete(row.petbreedId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹框 -->
    <el-dialog title="宠物品种信息" :visible.sync="dialogFormVisible">
      <el-form :model="form" align="left">
        <el-form-item label="宠物种类" :label-width="formLabelWidth" align="left">
          <el-select v-model="form.pettypeName" placeholder="请选择宠物种类">
            <el-option v-for="item in typeOptions" :key="item.pettypeId" :label="item.pettypeName"
              :value="item.pettypeId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="宠物品种" :label-width="formLabelWidth">
          <el-input v-model="form.petbreedName" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogStatus === 'create' ? createData() : updateData()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { selectAll, add, update, del, searchByName } from '../../api/petmanage/petbreed.js'
import { selectAll as selectAllpetType } from '../../api/petmanage/pettype.js'
export default {
  data() {
    return {
      search: '',
      dialogFormVisible: false,
      dialogStatus: '',
      updateId: null,
      formLabelWidth: '100px',
      petBreedList: [
        {
          petbreedId: 1,
          petbreedName: '狗',
          pettypeName: '哈士奇',
        }
      ],
      form: {
        petbreedId: '',
        petbreedName: '',
        pettypeName: '',
      },
      typeOptions: [{ pettypeId: '', pettypeName: '' }],

    }
  },
  methods: {
    resetForm() {
      this.form = {};
    },
    handleCreate() {
      this.resetForm();
      this.dialogStatus = 'create';
      this.dialogFormVisible = true;
    },
    // 新增
    async createData() {
      let res = await add(this.form);
      console.log(res)
      this.$notify({
        title: 'Success',
        message: 'Created Successfully',
        type: 'success',
        duration: 2000
      })
      this.dialogFormVisible = false;
      this.selectAll_();
    },
    handleUpdate(row) {
      this.form = Object.assign({}, row)
      this.dialogFormVisible = true;
      this.dialogStatus = 'update';
      this.updateId = row.petbreedId;
    },
    // 更新
    async updateData() {
      this.form.petbreedId = this.updateId;
      let res = await update(this.form);
      this.dialogFormVisible = false
      this.$notify({
        title: 'Success',
        message: 'Update Successfully',
        type: 'success',
        duration: 2000
      })
      this.petBreedList = res.data
    }, // 删除
    async handleDelete(id) {
      console.log(id)
      let res = await del(id);
      this.$notify({
        title: 'Success',
        message: 'Delete Successfully',
        type: 'success',
        duration: 2000
      })
      this.selectAll_();
    },
    // 查询所有
    async selectAll_() {
      let res = await selectAll();
      console.log(res)
      this.petBreedList = res.data
    },
    // 查询所有宠物类型的信息
    async selectAllpetType_() {
      let res = await selectAllpetType();
      console.log(res)
      this.typeOptions = res.data
    },
    // 根据姓名查询
    async searchByName_(params) {
      let res = await searchByName(params);
      console.log(res)
      this.petList = res;
    },

  },
  created() {
    this.selectAll_()
    this.selectAllpetType_();
  },

}
</script>

<style></style>